package jzoffer;

/**
 * 矩阵中的路径
 * 请设计一个函数，用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始，每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格，那么该路径不能再次进入该格子。例如，在下面的3×4的矩阵中包含一条字符串“bfce”的路径（路径中的字母用加粗标出）。
 *
 * [["a","b","c","e"],
 * ["s","f","c","s"],
 * ["a","d","e","e"]]
 *
 * 但矩阵中不包含字符串“abfb”的路径，因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后，路径不能再次进入这个格子。
 *
 * @author MaoLin Wang
 * @date 2020/8/29 10:01 下午
 */
public class PathInMatrix {

    public static boolean exist(char[][] board, String word) {
        if (word==null || "".equals(word.trim())){
            return false;
        }
        int y=board.length;
        int count=0;
        char[] chars = word.toCharArray();
        int[] temp=new int[]{-1,-1};
        boolean result=true;
        boolean[][]marked=new boolean[y][board[0].length];
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (marked[i][j]==true){
                    continue;
                }
                if (count==chars.length||result==false){
                    break;
                }
                if (count<chars.length && chars[count]==board[i][j]){
                    if (temp[0]==-1 && temp[1]==-1){
                        temp[0]=i;
                        temp[1]=j;
                        count++;
                        marked[i][j]=true;
                    }else if (i-temp[0]<=1 && (j-temp[1])<=1){
                        count++;
                        temp[0]=i;
                        temp[1]=j;
                        marked[i][j]=true;
                    }else {
                        result=false;
                    }
                }
            }
            for (int j = board[i].length-1; j >=0 ; j--) {
                if (marked[i][j]==true){
                    continue;
                }
                if (count==chars.length||result==false){
                    break;
                }
                if (count<chars.length && chars[count]==board[i][j]){
                    if (temp[0]==-1 && temp[1]==-1){
                        temp[0]=i;
                        temp[1]=j;
                        count++;
                        marked[i][j]=false;
                    }else if (i-temp[0]<=1 && (j-temp[1])<=1){
                        count++;
                        temp[0]=i;
                        temp[1]=j;
                        marked[i][j]=false;
                    }else {
                        result=false;
                    }
                }
            }
        }

        return result;
    }

    public static boolean exist2(char[][] board, String word) {
        char[] words = word.toCharArray();
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                if(dfs(board, words, i, j, 0)) return true;
            }
        }
        return false;
    }
    /*static boolean  dfs(char[][] board, char[] word, int i, int j, int k) {
        if(i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k]) return false;
        if(k == word.length - 1) return true;
        char tmp = board[i][j];
        board[i][j] = '/';
        boolean res = dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i - 1, j, k + 1) ||
                dfs(board, word, i, j + 1, k + 1) || dfs(board, word, i , j - 1, k + 1);
        board[i][j]=tmp;
        return res;
    }*/

    public static boolean exist3(char[][]board,String word){
        char[]words=word.toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (dfs(board,words,i,j,0)){
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean dfs(char[][] board, char[] words, int i, int j, int count) {
        if (i<0 || i>=board.length || j<0 || j>=board[0].length ||board[i][j] != words[count]){
            return  false;
        }
        if (count==words.length-1){
            return true;
        }
        char temp=board[i][j];
        board[i][j]='/';
        boolean res=(dfs(board,words,i+1,j,count+1) || dfs(board,words,i-1,j,count+1) ||
                dfs(board,words,i,j+1,count+1) || dfs(board,words,i,j-1,count+1));
        board[i][j]=temp;
        return res;
    }

    public static void main(String[] args) {
        char[][]chars=new char[3][4];
        chars[0][0]='A';
        chars[0][1]='B';
        chars[0][2]='C';
        chars[0][3]='E';
        chars[1][0]='S';
        chars[1][1]='F';
        chars[1][2]='C';
        chars[1][3]='S';
        chars[2][0]='A';
        chars[2][1]='D';
        chars[2][2]='E';
        chars[2][3]='E';

        boolean bfce = exist3(chars, "BCCED");
        System.out.println(bfce);
    }
}
